/**
 *  @author Emirald Mateli 
 */

ajax = 
{
    connection_fail: 
        function ()
        {
            return "Unable to complete request";
        },
        
    please_wait_text: 
        function ()
        {
            return '<img border=0 align="absmiddle" src="img/wait.gif"" />Please wait ...';
        },    
        
    /**
     * Creates a cross-browser XMLHttpRequest 
     */
    create :
        function()
        {
            // Kontrollojme browsers
            if (window.XMLHttpRequest) 
            {
                try
                {
                    return new XMLHttpRequest();
                }
                catch(e)
                {
                    return false;
                }
            }
            else 
                // Ne rast se kemi IE
                if (window.ActiveXObject) 
                {   
                    try 
                    {
                        return new ActiveXObject ("Microsoft.XMLHTTP");
                    }
                    catch (e) 
                    { 
                        // Kthejme false ne rast deshtimi
                        return false;
                    }
                }
        },

    /**
     * send a get request to the url defined in 'url' parameter
     * 
     * callback is the function to call when finished 
     */
    get : 
        function(url, callback)
        {
            ajaxReq = this.create();
            if (ajaxReq)
            {
                ajaxReq.onreadystatechange = 
                    function()
                    {
                        if ((ajaxReq.readyState == 4) && (callback))
                            callback(
                                (ajaxReq.status == 200) ? ajaxReq.responseText : false);
                    };
                ajaxReq.open("GET", url, true);
                ajaxReq.send(null);
            }
            else
                // Ne rast se nuk krijojme dot objektin kthejme false te thirresi me ane te callback
                if (callback)
                    callback(false);
        },
        
        
    /**
     * send a post request to 'url' with post parameters 'params'
     * 
     * callback is the function to call when request is finished 
     */
    post : 
        function(url, params, callback)
        {    
            ajaxReq = this.create();
            if (ajaxReq)
            {
                ajaxReq.onreadystatechange = 
                    function()
                    {
                        if ((ajaxReq.readyState == 4) && (callback))
                            callback(
                                (ajaxReq.status == 200) ? ajaxReq.responseText : false);
                    };
                    
                ajaxReq.open("POST", url, true);  
                ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                ajaxReq.setRequestHeader("Content-length", params.length);

                    
                ajaxReq.send(params);
            }
            else
                // Ne rast se nuk krijojme dot objektin kthejme false te thirresi me ane te callback
                if (callback)
                    callback(false);
        },
        
    /**
     * sends a get request to the url in the parameter
     * and alerts the result 
     */
    echoRequest :
        function(url)
        {
            ajax.get(url,
                function (result)
                {
                    if (result)
                        alert(result);
                    else
                        alert(ajax.connection_fail());
                })
        },
        
        
    /**
     * issues a get request.
     * 
     * sets the html of 'eleId' to an image + Please wait text
     * while it is still loading.
     * 
     * then the html is set to that of the request response
     * 
     * if failed will restore the html it previously had 
     * 
     * appendStr is a string that can be appended to the end of
     * the response text
     */
    htmlFromRequest :
        function(eleId, fromUrl, appendStr)
        {
            oldHtml = getHTML(eleId);
            setHTML(eleId, ajax.please_wait_text());
            ajax.get(fromUrl,
                function(result)
                {
                    if (result)
                    {
                        if (appendStr != undefined)
                            result += appendStr;
                        
                        setHTML(eleId, result);               
                    }
                    else
                    {
                        alert(ajax.connection_fail());
                        setHTML(eleId, oldHtml);
                    }
                });
        },
        
    /**
     * same as htmlFromRequest 
     */
    htmlFromPostRequest :
        function(eleId, fromUrl, params, appendStr)
        {
            oldHtml = getHTML(eleId);
            setHTML(eleId, ajax.please_wait_text());
            
            ajax.post(fromUrl, params,
                function(result)
                {
                    if (result)
                    {
                        if (appendStr != undefined)
                            result += appendStr;
                            
                        document.write(result);
                        return
                        
                        setHTML(eleId, result);               
                    }
                    else
                    {
                        alert(ajax.connection_fail());
                        setHTML(eleId, oldHtml);
                    }
                });
        }  
}

function setHTML(id, newHtml)
{
    document.getElementById(id).innerHTML = newHtml;
}

function getHTML(id, newHtml)
{
    return document.getElementById(id).innerHTML;
}